package saxion.speelveld.spaceengineer;

import saxion.speelveld.spaceengineer.model.Building;
import saxion.speelveld.spaceengineer.model.BuildingTile;
import saxion.speelveld.spaceengineer.model.ProducingBuilding;
import saxion.speelveld.spaceengineer.model.ResourceWrapper;
import saxion.speelveld.spaceengineer.model.Tile;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomUpgradePopup extends Activity
{

	private LinearLayout llUpgradePopupContainer;

	private ImageView ivUpgradeBuildingImage;

	private TextView tvUpgradeBuildingTitle;

	private TextView tvUpgradeConfirmationText;

	private Button btnAcceptUpgrade;

	private Button btnStopUpgrade;

	private ResourceWrapper resourceArray;

	private Tile selectedTile;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_custom_upgrade_popup);

		finishActivityWithCancelOnClickListener finishCancelOnClick = new finishActivityWithCancelOnClickListener();

		llUpgradePopupContainer = (LinearLayout) findViewById(R.id.llUpgradePopupContainer);
		ivUpgradeBuildingImage = (ImageView) findViewById(R.id.ivUpgradeBuildingImage);
		tvUpgradeBuildingTitle = (TextView) findViewById(R.id.tvUpgradeBuildingTitle);
		tvUpgradeConfirmationText = (TextView) findViewById(R.id.tvUpgradeConfirmationText);
		btnAcceptUpgrade = (Button) findViewById(R.id.btnAcceptUpgrade);
		btnStopUpgrade = (Button) findViewById(R.id.btnStopUpgrade);

		llUpgradePopupContainer.setOnClickListener(finishCancelOnClick);
		btnStopUpgrade.setOnClickListener(finishCancelOnClick);
		btnAcceptUpgrade.setOnClickListener(new finishActivityWithAcceptOnClickListener());

		btnAcceptUpgrade.setText("Upgrade");
		btnStopUpgrade.setText("Cancel");

		Object objResources = getIntent().getSerializableExtra("GameScreen.Resources");
		if (objResources != null)
		{
			resourceArray = (ResourceWrapper) objResources;
		}
		Object objTile = getIntent().getSerializableExtra("GameScreen.Tile");
		if (objTile != null)
		{
			selectedTile = (Tile) objTile;
		}

		if (resourceArray == null || selectedTile == null)
		{
			stopActivityResultCanceled();
		}

		if (selectedTile instanceof BuildingTile)
		{
			BuildingTile buildingTile = ((BuildingTile) selectedTile);
			Building selectedBuilding = buildingTile.getBuilding();
			ivUpgradeBuildingImage.setImageResource(selectedBuilding.getPreviewResourceImageId());
			tvUpgradeBuildingTitle.setText("Upgrade " + selectedBuilding.getBuildingName());
			int buildingCosts = 25;
			if (selectedBuilding instanceof ProducingBuilding)
			{
				tvUpgradeConfirmationText.setText("Upgrading this building costs " + buildingCosts
					+ " science points, and adds 1 resource gain per tick");
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.custom_upgrade_popup, menu);
		return true;
	}

	private class finishActivityWithAcceptOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			Intent returnIntent = new Intent();
			returnIntent.putExtra("Upgrading.SciencePointCosts", 25);
			setResult(RESULT_OK, returnIntent);
			finish();
		}
	}

	private void stopActivityResultCanceled()
	{
		Intent returnIntent = new Intent();
		setResult(RESULT_CANCELED, returnIntent);
		finish();
	}

	private class finishActivityWithCancelOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			stopActivityResultCanceled();
		}
	}

}
